package com.yk._06_Concurrent的三个工具类;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;

public class _02CyclicBarrier {
    public static void main(String[] args) {

        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, new Thread(() -> {
            System.out.println("猪八戒许愿完成,得到了布玛的内裤");
        }));

        for (int i = 1; i <= 7; i++) {
            final int temp = i;
            int finalI = i;
            new Thread(()->{
                try {
                    TimeUnit.SECONDS.sleep(finalI );
                    System.out.println("猪八戒集齐了"+String.valueOf(temp)+"龙珠");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            },"猪八戒-"+ i).start();
        }

    }
}
